<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <a-spin :spinning="isLoading">
      <a-form :form="form" @submit="handleSubmit">
        <a-row>
          <a-col :span="12">
            <a-form-item label="人脸识别" :labelCol="labelCol" :wrapperCol="{span:18}">
              <a-radio-group
                v-decorator="['is_face',{initialValue:0,rules: [{ required: true }]}
            ]"
              >
                <a-radio :value="0">
                  关闭
                </a-radio>
                <a-radio :value="1">
                  仅拍照
                </a-radio>
                <a-radio :value="2">
                  人脸比对
                </a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="API密钥（key）" :labelCol="labelCol" :wrapperCol="{span:10}">
              <a-input
                placeholder="请填写人脸识别密钥"
                v-decorator="[
              'face_key',{rules: [{ required: true, message: '请填写人脸识别密钥' }], validateTrigger: 'change'}
            ]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              label="考试需满足学时"
              :labelCol="labelCol"
              :wrapperCol="{span:10}"
              extra="考试不限制学时，请留空或填0">
              <a-input
                suffix="分钟"
                placeholder="请填写需满足学时"
                v-decorator="[
              'class_hour',{rules: [{ required: true, message: '请填写满足学时' }], validateTrigger: 'change'}
            ]"
              />
            </a-form-item>
          </a-col>
          <a-divider orientation="left">
            基础信息
          </a-divider>
          <a-col :span="12">
            <a-form-item label="测评须知内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <Ueditor
                v-decorator="['cpxznr']"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="考试承诺书" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <Ueditor
                v-decorator="['kscrs']"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <div style="text-align: center">
              <a-button size="large" style="width: 100px" type="primary" html-type="submit">保存</a-button>
            </div>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/setting/store'
import { isEmpty } from '@/utils/util'
import { Ueditor } from '@/components'

export default {
  components: {
    Ueditor
  },
  data () {
    return {
      // 当前设置项的key
      key: 'h5home',
      // 标签布局属性
      labelCol: { span: 6 },
      // 输入框布局属性
      wrapperCol: { span: 6 },
      // loading状态
      isLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 单选框样式
      radioStyle: {
        display: 'block',
        height: '30px',
        lineHeight: '30px'
      },
      // 当前记录详情
      record: {}
    }
  },
  // 初始化数据
  created () {
    // 获取当前详情记录
    this.getDetail()
  },
  methods: {

    // 获取当前详情记录
    getDetail () {
      this.isLoading = true
      Api.detail(this.key)
        .then(result => {
          // 当前记录
          this.record = result.data.values
          // 设置默认值
          this.setFieldsValue()
        })
        .finally(result => {
          this.isLoading = false
        })
    },

    decodeHtml (html) {
      var txt = document.createElement('textarea')
      txt.innerHTML = html
      return txt.value
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { record, $nextTick, form } = this
      !isEmpty(form.getFieldsValue()) && $nextTick(() => {
        if (record.cpxznr) {
          record.cpxznr = this.decodeHtml(record.cpxznr)
        }

        if (record.kscrs) {
          record.kscrs = this.decodeHtml(record.kscrs)
        }
        form.setFieldsValue(pick(record, ['is_face', 'face_key', 'class_hour', 'cpxznr', 'kscrs']))
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.isLoading = true
      Api.update(this.key, { form: values })
        .then((result) => {
          // 显示提示信息
          this.$message.success(result.message, 1.5)
        })
        .finally((result) => {
          this.isLoading = false
        })
    }

  }
}
</script>
<style lang="less" scoped>
/deep/ .ant-form-item-control {
  padding-left: 10px;

  .ant-form-item-control {
    padding-left: 0;
  }
}

.ant-divider {
  margin-top: 60px !important;
}

.ant-input-number {
  width: 160px;
}

.input-text_right {
  margin-left: 10px;
}

.radio-item {
  margin-bottom: 16px;
}
</style>
